Import mojo
Import agfx

Const OBJECT_MODE_RECT:Int=0
Const OBJECT_MODE_TILE:Int=1

Class TTiled_ObjectGroupObject

	Field name:String
	Field mode:Int=OBJECT_MODE_RECT
	Field type:String = "none"
	Field x:Int
	Field y:Int
	Field gid:Int = -1
	Field id:Int = -1
	Field tileset_id:Int = -1
	Field width:Int
	Field height:Int
	Field tilewidth:Int
	Field tileheight:Int
	Field atlas:TImage
	Field properties:TProperties
	
	'------------------------------------------------------------------------------------
	'
	'------------------------------------------------------------------------------------

	Method New(node:XMLElement)
	
		If DEBUG Then Print("    <OBJECT>")
		
		Self.properties		= New TProperties()		
		
		Self.name = node.GetAttribute("name")
		If DEBUG Then Print("     name: "+Self.name)
		
		If node.HasAttribute("type") Then 
			Self.type = node.GetAttribute("type")
			Self.mode = OBJECT_MODE_TILE
		End If
		If DEBUG Then Print("     type: "+Self.type)
		
		Self.x = Int(node.GetAttribute("x"))
		If DEBUG Then Print("        x: "+Self.x)
		
		Self.y = Int(node.GetAttribute("y"))
		If DEBUG Then Print("        y: "+Self.y)
		
		Self.width = Int(node.GetAttribute("width"))
		If DEBUG Then Print("       width: "+Self.width)

		Self.height = Int(node.GetAttribute("height"))
		If DEBUG Then Print("       height: "+Self.height)

		If node.HasAttribute("gid") Then Self.gid = Int(node.GetAttribute("gid"))
		If DEBUG Then Print("      gid: "+Self.gid)
		
		Local prop:XMLElement
		For prop = EachIn node.Children
			
			Select prop.Name()
				Case "properties"
					If DEBUG Then Print("      <PROPERTIES>")
					Self.properties.Add(prop,"property","name","value")
			End Select
			
			
		Next
		
	End Method

	'------------------------------------------------------------------------------------
	' Draw Object
	'------------------------------------------------------------------------------------
	Method Draw(off_x:Int,off_y:Int)
		if (Self.mode=OBJECT_MODE_TILE) Then
			Self.atlas.DrawTile(Self.x-off_x,Self.y-off_y-Self.tileheight,Self.id,0,1,1)			
		End If

		if (Self.mode=OBJECT_MODE_RECT) Then
			SetColor(255,0,0)
			DrawText(Self.name,Self.x-off_x+4,Self.y-off_y+4)
			DrawRectOutline(Self.x-off_x,Self.y-off_y,Self.width,Self.height)
			SetColor(255,255,255)
		End If

	End Method
	
End Class